/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mx.itschapala.dsw2.vista;

import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;
import mx.itschapala.dsw2.biblioteca8a.BL.AutorBLLocal;
import mx.itschapala.dsw2.biblioteca8a.modelo.Autor;

/**
 *
 * @author Etts
 */
@ManagedBean
@SessionScoped
public class AuthorBean {
    @EJB
    private AutorBLLocal autorBL;
    private List<Autor>lista;
    private Autor author;
    private String accion;
    private int n;
    

    /**
     * Creates a new instance of AuthorBean
     */
    public AuthorBean() {
        author=new Autor();
    }

    public List<Autor> getLista() {
        lista = autorBL.getAll();
        return lista;
        
    }

    public Autor getAuthor() {
        return author;
    }

    public void setAuthor(Autor author) {
        this.author = author;
    }

    //-----------------acciones---------------------
    public String procesarAccion() {
        if(accion.equalsIgnoreCase("Nuevo")){
        autorBL.register(author);
        }
        else
            if(accion.equalsIgnoreCase("Editar")){
                autorBL.modify(author);
            }
            else if(accion.equalsIgnoreCase("Eliminar")){
                autorBL.delete(author);
            }
        accion="";
     return "Autor";
    }
    
    public String limpiarAccion(){
        author=new Autor();
        author.setNombre("eeeee");
        return "AutorCrearEditar";
    }
    
    //--------------------listener--------------------------------
    
    public void prepararNuevo(ActionEvent evt){
author=new Autor();
accion="Nuevo";
}
public void prepararEditar(ActionEvent evt)   
{
System.out.println(evt.getComponent().getClientId());
 n=Integer.parseInt(evt.getComponent().getClientId().split(":")[3]);
    System.out.println("el numero es:"+n);
    author=lista.get(n);
    accion="Editar";
}     


public void prepararEliminar(ActionEvent evt)   
{
System.out.println(evt.getComponent().getClientId());
 n=Integer.parseInt(evt.getComponent().getClientId().split(":")[3]);
    System.out.println("el numero es:"+n);
    author=lista.get(n);
    accion="Eliminar";
}
    
}
